package conf

import (
	"flag"
	"github.com/sirupsen/logrus"
	"os"
	"path/filepath"
)

func getRootDir() string {
	// 获取可执行文件的路径
	executable, err := os.Executable()
	if err != nil {
		return ""
	}

	// 获取可执行文件所在的目录
	execDir := filepath.Dir(executable)
	return execDir
}

type Args struct {
	LogDir     string       // LogDir 日志存放路径
	TmpDir     string       // TmpDir 临时文件目录
	LogLevel   logrus.Level // LogLevel 日志等级
	ServiceDir string       // ServiceDir 守护进程配置目录
}

func ParseArg() Args {
	var logDir string
	flag.StringVar(&logDir, "log-dir", "logs/", "日志存放目录")

	var tmpDir string
	flag.StringVar(&tmpDir, "tmp-dir", filepath.Join(getRootDir(), "tmp"), "临时文件存放目录")

	var level string
	flag.StringVar(&level, "log-level", "debug", "日志级别")

	flag.Parse()

	var logLevel logrus.Level
	switch level {
	case "debug":
		logLevel = logrus.DebugLevel
	case "info":
		logLevel = logrus.InfoLevel
	case "warn":
		logLevel = logrus.WarnLevel
	case "error":
		logLevel = logrus.ErrorLevel
	case "fatal":
		logLevel = logrus.FatalLevel
	case "panic":
		logLevel = logrus.PanicLevel
	}

	args := Args{
		LogDir:     logDir,
		TmpDir:     tmpDir,
		LogLevel:   logLevel,
		ServiceDir: "/etc/systemd/system/",
	}
	return args
}

var Conf = ParseArg()
